//
//  CatchViewController.m
//  Animation
//
//  Created by Angela on 16/8/11.
//  Copyright © 2016年 Angela. All rights reserved.
//捕捉行为

#import "CatchViewController.h"

@interface CatchViewController ()
@property (strong, nonatomic)  UIView *blueView;
@property(nonatomic,strong)UIDynamicAnimator *animator;
@end
@implementation CatchViewController
-(UIDynamicAnimator *)animator
{
    if (_animator==nil) {
        //创建物理仿真器，设置仿真范围，ReferenceView为参照视图
        _animator=[[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    }
    return _animator;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    self.blueView=[[UIView alloc]initWithFrame:CGRectMake(0, 100, 60,60)];
    self.blueView.backgroundColor=[UIColor blueColor];
    [self.view addSubview:self.blueView];
    
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //获取一个触摸点
    UITouch *touch=[touches anyObject];
    CGPoint point=[touch locationInView:touch.view];
    
    //1.创建捕捉行为
    //需要传入两个参数：一个物理仿真元素，一个捕捉点
    UISnapBehavior *snap=[[UISnapBehavior alloc]initWithItem:self.blueView snapToPoint:point];
    //设置防震系数（0~1，数值越大，震动的幅度越小）
    snap.damping=arc4random_uniform(10)/10.0;
    
    //2.执行捕捉行为
    //注意：这个控件只能用在一个仿真行为上，如果要拥有持续的仿真行为，那么需要把之前的所有仿真行为删除
    //删除之前的所有仿真行为
    [self.animator removeAllBehaviors];
    [self.animator addBehavior:snap];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
